package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    NursingLevelMapper nursingLevelMapper;

    @Override
    public PageBean<NursingPlanVo> getNursingPlansByPage(NursingProjectPageQueryDto pageQueryDto) {
        PageHelper.startPage(pageQueryDto.getPageNum(), pageQueryDto.getPageSize());//Page本质就是List
        return PageBean.of(nursingPlanMapper.getNursingPlansByPage(pageQueryDto), NursingPlanVo.class);
    }

    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        //添加一条护理计划
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
       /* plan.setCreateTime(LocalDateTime.now());
        plan.setUpdateTime(LocalDateTime.now());
        plan.setCreateBy(1671403256519078138L);
        plan.setUpdateBy(1671403256519078138L);*/

        plan.setStatus(1); //0禁用、1启用

        nursingPlanMapper.add(plan); //注意：设置主键自增返回

        //2. 添加多条护理计划护理项目关系
        List<NursingProjectPlanDto> dtoList = nursingPlanDto.getProjectPlans();
        List<NursingProjectPlan> poList = BeanUtil.copyToList(dtoList, NursingProjectPlan.class);

        poList.forEach(projectPlan -> {
            //必须要设置projectPlan的plan_id
            projectPlan.setPlanId(plan.getId());
            //添加操作
            nursingProjectPlanMapper.add(projectPlan);
        });
    }

    @Override
    public NursingPlanVo read(Long id) {

        //1. 根据id查询护理计划基本信息
        NursingPlan nursingPlan = nursingPlanMapper.read(id);
        //2. 根据id查询护理计划对应的所有护理项目
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlanMapper.getByPlanId(id);

        //3. 将两部分数据封装到 NursingPlanVo 中
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    @Transactional
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        //1. 根据id修改护理计划基本信息
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
       /* plan.setUpdateTime(LocalDateTime.now());
        plan.setUpdateBy(1671403256519078138L);*/
        nursingPlanMapper.update(plan);
        //2. 根据id修改护理计划对应的护理项目关系
        //2.1. 根据id删除对应的所有护理项目关系
        nursingProjectPlanMapper.deleteByPlanId(plan.getId());
        //2.2. 重新添加关系
        List<NursingProjectPlanDto> dtoList = nursingPlanDto.getProjectPlans();
        List<NursingProjectPlan> poList = BeanUtil.copyToList(dtoList, NursingProjectPlan.class);
        poList.forEach(projectPlan -> {
            projectPlan.setPlanId(plan.getId());
            //添加操作
            nursingProjectPlanMapper.add(projectPlan);
        });
    }

    @Transactional
    @Override
    public void delete(Long id) {
        if (nursingPlanMapper.read(id).getStatus() == 1) {

            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        //1. 根据id查询护理计划是否关联了护理等级
        List<NursingLevel> list = nursingLevelMapper.ByPlanId(id);
        if (CollUtil.isNotEmpty(list)) {//原始方法size>0&&!=null
            //说明有关联数据
            throw new BaseException(BasicEnum.DELETE_PROJECT_FAIL);
        }
        nursingPlanMapper.delete(id);

        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        /*NursingPlan nursingPlan = new NursingPlan(); 普通方式创建NursingPlan对象
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);*/
        NursingPlan nursingPlan = NursingPlan.builder().id(id).status(status).build();
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public List<NursingPlan> findAll() {

        return nursingPlanMapper.findAll();
    }
}


